package com.zzuli.practice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.entity.Result;
import com.zzuli.practice.entity.User;
import com.zzuli.practice.service.impl.LoginService;
import com.zzuli.practice.service.impl.WorkerService;
import com.zzuli.practice.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/worker")
public class WorkerController implements Constant {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private LoginService loginService;

    @RequestMapping({"/pageworker/{pageIndex}", "/pageworker"})
    public Result getPageWorkerList(@PathVariable(value = "pageIndex", required = false) Integer pageIndex){
        Result result = new Result();
        try{
            IPage<Repairman> iPage = workerService.pageWorkList(pageIndex);
            if (iPage != null && iPage.getSize() > 0){
                result.setStatusCode(SUCCESS);
                result.setMessage("success");
                result.setData(iPage);
            } else {
                result.setStatusCode(UNFIND);
                result.setMessage("没有数据");
            }
        } catch (Exception e){
            result.setStatusCode(UNFIND);
            result.setMessage("系统异常");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "/insertworker", method = RequestMethod.POST)
    public Result insertWorker(String name, String age, String sex, String phone, String username){
        Result result = new Result();

        // 判断数据合法性
        if (StringUtils.isBlank(name)){
            result.setStatusCode(FAILURE);
            result.setMessage("姓名不能为空");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(sex)){
            result.setStatusCode(FAILURE);
            result.setMessage("性别不能为空");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(age)){
            result.setStatusCode(FAILURE);
            result.setMessage("年龄不能为空");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(phone)){
            result.setStatusCode(FAILURE);
            result.setMessage("电话不能为空");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(username)){
            result.setStatusCode(FAILURE);
            result.setMessage("您还未登录");
            result.setData(null);
            return result;
        }

        // 权限判断
        User user = loginService.selectByUserName(username);
        if (user.getType() == null || user.getType() == 0){
            result.setStatusCode(FAILURE);
            result.setMessage("您没有权限");
            result.setData(null);
            return result;
        }

        // 查询员工是否存在
        Repairman repairman1 = workerService.selectByName(name);
        if (repairman1 != null){
            result.setStatusCode(FAILURE);
            result.setMessage("该维修员已存在");
            result.setData(null);
            return result;
        }

        // 添加数据
        Repairman repairman = new Repairman();
        repairman.setName(name);
        repairman.setAge(age);
        repairman.setSex(sex);
        repairman.setPhone(phone);
        repairman.setStatus(0);
        int i = workerService.insertWorker(repairman);

        if (i == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("添加成功");
            result.setData(null);
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("添加失败");
            result.setData(null);
            return result;
        }

    }

    @RequestMapping("/selectusertype/{username}")
    public Result findUserType(@PathVariable("username") String username){
        Result result = new Result();

        if (username == null){
            result.setStatusCode(FAILURE);
            result.setMessage("您还未登录");
            return result;
        }

        User user = loginService.selectByUserName(username);
        System.out.println();
        System.out.println(user.getType());
        if (user.getType() == null || user.getType() == 0){
            result.setStatusCode(FAILURE);
            result.setMessage("您没有权限");
            return result;
        } else {
            result.setStatusCode(SUCCESS);
            return result;
        }
    }

    @RequestMapping(value = "/deleteworker/{id}", method = RequestMethod.POST)
    public Result deleteWorker(@PathVariable("id") Integer id, String username){
        Result result = new Result();

        if (username == null){
            result.setStatusCode(FAILURE);
            result.setMessage("您还未登录");
            return result;
        }
        if (id == null){
            result.setStatusCode(FAILURE);
            result.setMessage("数据异常");
            return result;
        }

        User user = loginService.selectByUserName(username);
        if (user.getType() == null || user.getType() == 0){
            result.setStatusCode(FAILURE);
            result.setMessage("您没有权限");
            return result;
        }

        // 删除维修员
        int i = workerService.deleteById(id);

        if (i != 1){
            result.setStatusCode(FAILURE);
            result.setMessage("删除失败");
            return result;
        } else {
            result.setStatusCode(SUCCESS);
            result.setMessage("删除成功");
            return result;
        }

    }

    @RequestMapping(value = "/selectworkerbyid/{id}", method = RequestMethod.GET)
    public Result findWorkerById(@PathVariable("id") Integer id){
        Result result = new Result();

        if (id == null){
            result.setStatusCode(FAILURE);
            result.setMessage("数据异常");
            return result;
        }

        Repairman repairman = workerService.selectById(id);
        if (repairman == null){
            result.setStatusCode(FAILURE);
            result.setMessage("该维修员不存在");
            return result;
        }

        result.setStatusCode(SUCCESS);
        result.setMessage("查询成功");
        result.setData(repairman);
        return result;

    }

    @RequestMapping(value = "/updatebyid", method = RequestMethod.POST)
    public Result updateById(Integer id, String name, String sex, String age, String phone, Integer status){
        Result result = new Result();

        if (id == null){
            result.setStatusCode(FAILURE);
            result.setMessage("数据异常");
            return result;
        }
        Repairman repairman = new Repairman();
        repairman.setId(id);
        repairman.setName(name);
        repairman.setAge(age);
        repairman.setPhone(phone);
        repairman.setStatus(status);

        int i = workerService.updateById(id, repairman);

        if (i == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("修改成功");
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("修改失败");
            return result;
        }

    }

}
